package com.example.myapplication.old;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.old.adapter.JokerAdapter;
import com.example.myapplication.old.api.ApiService;
import com.example.myapplication.old.entity.Joker;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class RetrofitDemoActivity extends AppCompatActivity {
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit_demo);
        TextView appbar = findViewById(R.id.tittle);
        appbar.setText("笑话");
        recyclerView = findViewById(R.id.recycler_view_hotsong);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        JokerAdapter adapter = new JokerAdapter();
        recyclerView.setAdapter(adapter);


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiService service = retrofit.create(ApiService.class);
        Call<Joker> call = service.getJoke(10);
        call.enqueue(new Callback<Joker>() {
            @Override
            public void onResponse(Call<Joker> call, Response<Joker> response) {
                Toast.makeText(getApplicationContext(), "请求成功", Toast.LENGTH_SHORT).show();
                Joker joker = response.body();
                adapter.setData(joker.getJokes());
//                Joker joker = new Joker();
//                try {
//                    String jsonstr = response.body().string();
//                    Log.e("jsonStr", jsonstr);
//                    Gson gson = new Gson();
//                    joker = gson.fromJson(jsonstr, Joker.class);
//                    adapter.setData(joker.getJokes());
//
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
            }

            @Override
            public void onFailure(Call<Joker> call, Throwable t) {
                Toast.makeText(getApplicationContext(), "请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}